package com.mengshu.token.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ThreadLocalUtils {

    private static final ThreadLocal<ConcurrentHashMap<String, Object>> tl = new ThreadLocal<ConcurrentHashMap<String, Object>>(){
        protected ConcurrentHashMap<String, Object> initHashMap() {
            return new ConcurrentHashMap<String, Object>();
        }
    };

    public static Map<String, Object> get() {
        if (tl.get() == null) {
            return new ConcurrentHashMap<String, Object>();
        }
        return tl.get();
    }

    public static Object get(String key) {
        if (tl.get() == null) {
            return null;
        }
        return tl.get().get(key);
    }

    public static void set(String key, Object value) {
        if (tl.get() == null) {
            tl.set(new ConcurrentHashMap<String, Object>());
        }
        tl.get().put(key, value);
    }

    public static void clear() {
        tl.remove();
    }

    public static void clear(String key) {
        if (tl.get() != null) {
            tl.get().remove(key);
        }
    }
}
